<template>
  <!-- 移动端适配布局 -->
  <div :class="['app-container', isMobile ? 'mobile-device' : 'desktop-device']">
    <!-- 移动端路由视图 -->
    <router-view v-slot="{ Component }">
      <keep-alive :include="cachedViews">
        <component :is="Component" />
      </keep-alive>
    </router-view>

    <!-- 移动端底部导航栏 -->
    <van-tabbar v-if="isMobile && showTabBar" v-model="activeTab" route>
      <van-tabbar-item to="/new/home" icon="home-o">首页</van-tabbar-item>
      <van-tabbar-item to="/new/apps" icon="apps-o">应用</van-tabbar-item>
      <van-tabbar-item to="/new/profile" icon="user-o">我的</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
import { ref, computed, onMounted, onBeforeUnmount, watch } from 'vue'
import { useRoute } from 'vue-router'

export default {
  name: 'App',
  setup() {
    const isMobile = ref(true)
    const activeTab = ref(0)
    const route = useRoute()
    const cachedViews = ref(['MobileHome', 'MobileApplications', 'MobileProfile'])

    // 检测当前设备类型
    const checkDeviceType = () => {
      isMobile.value = window.innerWidth < 768
    }

    // 监听窗口大小变化
    onMounted(() => {
      checkDeviceType()
      window.addEventListener('resize', checkDeviceType)
    })

    onBeforeUnmount(() => {
      window.removeEventListener('resize', checkDeviceType)
    })

    // 监听路由变化，设置tabbar激活状态
    watch(() => route.path, (path) => {
      if (path.includes('/new/home')) {
        activeTab.value = 0
      } else if (path.includes('/new/apps')) {
        activeTab.value = 1
      } else if (path.includes('/new/profile')) {
        activeTab.value = 2
      }
    }, { immediate: true })

    // 控制是否显示底部导航栏
    const showTabBar = computed(() => {
      const hideTabBarRoutes = [
        '/new/finance',
        '/new/asset',
        '/new/material'
      ]
      
      // 检查当前路由是否在hideTabBarRoutes中或是其子路由
      return !hideTabBarRoutes.some(path => route.path.startsWith(path))
    })

    return {
      isMobile,
      activeTab,
      showTabBar,
      cachedViews
    }
  }
}
</script>

<style>
/* 全局样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background-color: #f7f8fa;
  color: #323233;
}

.app-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: column;
}

/* 移动设备样式 */
.mobile-device {
  max-width: 100%;
  padding-bottom: 50px;
}

/* 桌面设备样式 - 强制移动端布局 */
.desktop-device {
  margin: 0 auto;
  max-width: 450px; /* 限制最大宽度 */
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  height: 100vh;
  padding-bottom: 50px;
}
</style> 